package com.michael.study.jetpack

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.solver.GoalRow
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.work.BackoffPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import com.michael.study.jetpack.databinding.ActivityMainBinding
import com.michael.study.jetpack.extension.open
import com.michael.study.jetpack.module.browse.BrowseActivity
import com.michael.study.jetpack.module.coroutines.CoroutinesActivity
import com.michael.study.jetpack.module.databinding.DataBindingActivity
import com.michael.study.jetpack.module.dispatcher.DispatcherActivity
import com.michael.study.jetpack.module.dsl.DslActivity
import com.michael.study.jetpack.module.hint.HintActivity
import com.michael.study.jetpack.module.login.LoginActivity
import com.michael.study.jetpack.module.user.UserProfileActivity
import com.michael.study.jetpack.module.video.VideoActivity
import com.michael.study.jetpack.module.work.MainViewModel
import com.michael.study.jetpack.module.work.MainViewModelFactory
import com.michael.study.jetpack.module.work.SimpleWork
import com.michael.study.jetpack.utils.Actions
import kotlinx.android.synthetic.main.layout_toolbar.*
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityMainBinding
    private lateinit var mViewModel: MainViewModel
    private val isVisible = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        toolbar.title = "首页"
        binding.btnUser.setOnClickListener(this)
        binding.btnHint.setOnClickListener(this)
        binding.btnVideo.setOnClickListener(this)
        binding.btnDatabinding.setOnClickListener(this)
        binding.btnCoroutines.setOnClickListener(this)
        binding.btnGo.setOnClickListener(this)
        binding.btnDsl.setOnClickListener(this)
        if (!isVisible) {
            binding.btnUser.visibility = View.GONE
            binding.btnHint.visibility = View.GONE
            binding.btnWork.visibility = View.GONE
            binding.btnVideo.visibility = View.GONE
            binding.btnDatabinding.visibility = View.GONE
            binding.btnCoroutines.visibility = View.GONE
            binding.btnDsl.visibility = View.GONE
        }

        getSharedPreferences("data", Context.MODE_PRIVATE).open {
            putString("name", "Tome")
            putInt("age", 28)
            putBoolean("married", false)
        }

        mViewModel = ViewModelProvider(this, MainViewModelFactory(3)).get(MainViewModel::class.java)
        mViewModel.user.observe(this, Observer {
            Log.d("lgq", "observe: ${it?.name}")
        })
    }

    override fun onClick(v: View?) {
        if (v?.id == R.id.btn_user) {
            Actions.startActivity(this,LoginActivity::class.java)
        }
        if (v?.id == R.id.btn_work) {
            val request = OneTimeWorkRequest.Builder(SimpleWork::class.java)
                .setInitialDelay(5, TimeUnit.SECONDS)
                .addTag("simple")
                .setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
                .build()
            WorkManager.getInstance(this).enqueue(request)

            WorkManager.getInstance(this).cancelWorkById(request.id)
            WorkManager.getInstance(this).cancelAllWorkByTag("simple")
            WorkManager.getInstance(this).cancelAllWork()

        }

        if (v?.id == R.id.btn_hint) {
            Actions.startActivity(this,HintActivity::class.java)
        }
        if (v?.id == R.id.btn_video) {
            Actions.startActivity(this,VideoActivity::class.java)
        }

        if (v?.id == R.id.btn_databinding) {
            Actions.startActivity(this,DataBindingActivity::class.java)
        }

        if (v?.id == R.id.btn_coroutines) {
            Actions.startActivity(this, CoroutinesActivity::class.java)
        }

        if (v?.id == R.id.btn_dsl) {
            Actions.startActivity(this,DslActivity::class.java)
        }

        if (v?.id == R.id.btn_go) {
            Actions.startActivity(this, DispatcherActivity::class.java)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("lgq", "onDestroy: ")
    }
}
